/*
 * Powered By [Frank-Liz-Lee]
 * Copyright(C) 2012-2016 Liz Company
 * All rights reserved.
 */

package com.linln.modules.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linln.common.enums.StatusEnum;
import com.linln.modules.system.mapper.DictMapper;
import com.linln.modules.system.domain.Dict;
import java.util.List;

import com.linln.modules.system.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Transactional(rollbackFor = Exception.class)
@Service
public class DictServiceImpl  extends GenericService<Dict, Long>  implements DictService {

    private DictMapper dictMapper;

    @Autowired
    public void setDictMapper(DictMapper dictMapper) {
        this.dictMapper = dictMapper;
    }

    /**
     * 通过主键查询实体对象
     * @param primaryKey
     * @return
     * @
     */
    public Dict getByPK(Long primaryKey)  {
        return dictMapper.getByPK(primaryKey);
    }

    /**
     * 查询所有记录
     * @return
     * @
     */
    public List<Dict> list()  {
        return dictMapper.list();
    }

    /**
     * 根据查询条件查询所有记录
     * @return
     * @
     */
    public List<Dict> listByProperty(Dict dict) {
        return dictMapper.listByProperty(dict);
    }


    /**
     * 根据主键删除记录
     * @param primaryKey
     * @return
     * @
     */
    public int deleteByPK(Long primaryKey)  {
        return dictMapper.deleteByPK(primaryKey);
    }

    /**
     * 根据多个主键删除记录
     * @param primaryKeys
     * @
     */
    public int deleteByPKeys(List<Long> primaryKeys)  {
        return dictMapper.deleteByPKeys(primaryKeys);
    }

    /**
     * 根据传入参数删除记录
     * @param dict
     * @return
     * @
     */
    public int deleteByProperty(Dict dict)  {
        return dictMapper.deleteByProperty(dict);
    }

    /**
     * 保存记录
     * @param dict
     * @return
     * @
     */
    public int save(Dict dict)  {
        return dictMapper.save(dict);
    }

    /**
     * 更新记录
     * @param dict
     * @return
     * @
     */
    public int update(Dict dict) {
        return dictMapper.update(dict);
    }

    /**
     * 根据条件查询记录条数
     * @param dict
     * @return
     * @
     */
    public int findByCount(Dict dict)  {
        return dictMapper.findByCount(dict);
    }

    /**
     * 根据查询条件查询分页记录
     * @return
     */
    public PageInfo<Dict> findByPage(Integer pageNum, Integer pageSize, Dict dict) {
        PageHelper.startPage(pageNum,pageSize);
        List<Dict> dictList = dictMapper.listByProperty(dict);
        if(null == dictList || dictList.size() == 0){
            return new Page<Dict>().toPageInfo();
        }
        Page<Dict> page =  (Page<Dict>)dictList;
            return page.toPageInfo();
    }

    @Override
    public Dict getByNameOk(String label) {
        Dict dict = new Dict();
        dict.setName(label);
        dict.setStatus(StatusEnum.OK.getCode());
        List<Dict> dictList = dictMapper.listByProperty(dict);
        if(null == dictList || dictList.size() == 0){
            return null;
        }
        return dictList.get(0);
    }


    @Override
    public boolean repeatByName(Dict query) {
        Dict temp = new Dict();
        temp.setName(query.getName());
        List<Dict> list = dictMapper.listByProperty(temp);
        if(CollectionUtils.isEmpty(list)){
            return false;
        }
        Dict dict = list.get(0);
        if(query.getId() != null){
            return dict != null && dict.getId().equals(query.getId());
        }else{
            return dict != null;
        }
    }

    @Override
    public boolean updateStatus(StatusEnum statusEnum, List<Long> ids) {
        int result =  dictMapper.updateStatus(statusEnum.getCode(),ids);
        return result > 0;
    }
}
